package com.yfp.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.client.domain.ClientBill;
import com.yfp.client.domain.OrderGiftGold;
import com.yfp.client.domain.OrderList;
import com.yfp.client.domain.dto.StoredResultsDTO;
import com.yfp.client.mapper.OrderGiftGoldMapper;
import com.yfp.client.service.ClientBillService;
import com.yfp.client.service.OrderGiftGoldService;
import com.yfp.client.service.OrderListService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @description 针对表【order_gift_gold】的数据库操作Service实现
 * @createDate 2024-11-28 15:59:48
 */
@Service
public class OrderGiftGoldServiceImpl extends ServiceImpl<OrderGiftGoldMapper, OrderGiftGold> implements OrderGiftGoldService {

    @Resource
    private OrderGiftGoldMapper orderGiftGoldMapper;
    @Resource
    private ClientBillService clientBillService;
    @Resource
    private OrderListService orderListService;

    @Override
    public List<OrderGiftGold> listByOrderListId(String orderListId) {
        return orderGiftGoldMapper.selectByOrderListId(orderListId);
    }

    @Override
    public void addOrderGiftGold(List<OrderGiftGold> orderGiftGolds) {
        for (OrderGiftGold orderGiftGold : orderGiftGolds) {
            orderGiftGoldMapper.insert(orderGiftGold);
        }
    }

    @Override
    public List<OrderGiftGold> listByOrderId(String orderId) {
        List<OrderList> orderLists = orderListService.listByOrderSn(orderId);
        List<String> orderListIds = orderLists.stream().map(OrderList::getId).collect(Collectors.toList());
        return orderGiftGoldMapper.selectByOrderListIds(orderListIds);
    }

    @Override
    public List<OrderGiftGold> calculatePartialRefund(List<OrderGiftGold> orderGiftGoldList, StoredResultsDTO storedResultsDTO) {
        List<OrderGiftGold> orderGiftGolds = new ArrayList<>();
        orderGiftGoldList.stream().filter(item -> {
            boolean currentBillMoney = NumberUtil.isGreater(item.getMoney(), BigDecimal.ZERO);
            boolean currentBillGiftGold = NumberUtil.isGreater(item.getGiftGold(), BigDecimal.ZERO);
            return currentBillMoney || currentBillGiftGold;
        }).sorted(Comparator.comparing(OrderGiftGold::getId)).forEach(clientBill -> {
            boolean moreThanZero = NumberUtil.isLessOrEqual(storedResultsDTO.getMoney(), BigDecimal.ZERO);
            boolean giftMoneyThanZero = NumberUtil.isLessOrEqual(storedResultsDTO.getGiftMoney(), BigDecimal.ZERO);
            if (moreThanZero && giftMoneyThanZero) {
                return;
            }

            ClientBill clientBillTemp = BeanUtil.toBean(clientBill, ClientBill.class);

            // 消费当前账单的赠金
            if (NumberUtil.isGreater(storedResultsDTO.getGiftMoney(), BigDecimal.ZERO)) {
                if (NumberUtil.isGreaterOrEqual(clientBill.getGiftGold(), storedResultsDTO.getGiftMoney())) {
                    clientBill.setGiftGold(NumberUtil.sub(clientBill.getGiftGold(), storedResultsDTO.getGiftMoney()));
                    storedResultsDTO.setGiftMoney(BigDecimal.ZERO);
                } else {
                    storedResultsDTO.setGiftMoney(NumberUtil.sub(storedResultsDTO.getGiftMoney(), clientBill.getGiftGold()));
                    clientBill.setGiftGold(BigDecimal.ZERO);
                }
            }

            // 消费当前账单的金额
            if (NumberUtil.isGreater(storedResultsDTO.getMoney(), BigDecimal.ZERO)) {
                if (NumberUtil.isGreaterOrEqual(clientBill.getMoney(), storedResultsDTO.getMoney())) {
                    clientBill.setMoney(NumberUtil.sub(clientBill.getMoney(), storedResultsDTO.getMoney()));
                    storedResultsDTO.setMoney(BigDecimal.ZERO);
                } else {
                    storedResultsDTO.setMoney(NumberUtil.sub(storedResultsDTO.getMoney(), clientBill.getMoney()));
                    clientBill.setMoney(BigDecimal.ZERO);
                }
            }

            boolean b = NumberUtil.isGreater(storedResultsDTO.getMoney(), BigDecimal.ZERO) || NumberUtil.isGreater(storedResultsDTO.getGiftMoney(), BigDecimal.ZERO);
            boolean b1 = NumberUtil.isGreater(clientBill.getMoney(), BigDecimal.ZERO) || NumberUtil.isGreater(clientBill.getGiftGold(), BigDecimal.ZERO);
            if (b && b1) {

                // 消费当前账单的金额
                if (NumberUtil.isGreater(storedResultsDTO.getGiftMoney(), BigDecimal.ZERO)) {
                    if (NumberUtil.isGreaterOrEqual(clientBill.getMoney(), storedResultsDTO.getGiftMoney())) {
                        clientBill.setMoney(NumberUtil.sub(clientBill.getMoney(), storedResultsDTO.getGiftMoney()));
                        storedResultsDTO.setGiftMoney(BigDecimal.ZERO);
                    } else {
                        storedResultsDTO.setGiftMoney(NumberUtil.sub(storedResultsDTO.getGiftMoney(), clientBill.getMoney()));
                        clientBill.setMoney(BigDecimal.ZERO);
                    }
                }

                // 消费当前账单的赠金
                if (NumberUtil.isGreater(storedResultsDTO.getMoney(), BigDecimal.ZERO)) {
                    if (NumberUtil.isGreaterOrEqual(clientBill.getGiftGold(), storedResultsDTO.getMoney())) {
                        clientBill.setGiftGold(NumberUtil.sub(clientBill.getGiftGold(), storedResultsDTO.getMoney()));
                        storedResultsDTO.setMoney(BigDecimal.ZERO);
                    } else {
                        storedResultsDTO.setMoney(NumberUtil.sub(storedResultsDTO.getMoney(), clientBill.getGiftGold()));
                        clientBill.setGiftGold(BigDecimal.ZERO);
                    }
                }
            }

            OrderGiftGold orderGiftGold = OrderGiftGold.builder()
                    .orderListId(clientBill.getOrderListId())
                    .clientBillId(clientBill.getClientBillId())
                    .money(NumberUtil.sub(clientBillTemp.getMoney(), clientBill.getMoney()))
                    .giftGold(NumberUtil.sub(clientBillTemp.getGiftGold(), clientBill.getGiftGold()))
                    .build();
            orderGiftGolds.add(orderGiftGold);
        });
        return orderGiftGolds;
    }

    @Override
    public ClientBill lastRatio(List<OrderGiftGold> orderGiftGolds) {
        // 根据id去重
        List<OrderGiftGold> collect = orderGiftGolds.stream()
                .collect(Collectors.collectingAndThen(
                        Collectors.toMap(
                                OrderGiftGold::getClientBillId,
                                orderGiftGold -> orderGiftGold,
                                (existing, replacement) -> existing
                        ),
                        map -> new ArrayList<>(map.values())
                )).stream().sorted(Comparator.comparing(OrderGiftGold::getClientBillId)).collect(Collectors.toList());
        OrderGiftGold last = CollUtil.getLast(collect);
        return clientBillService.getById(last.getClientBillId());
    }
}




